#$Id$

package Feeds::GCF::Exception::ExcGFI;
@ISA = qw(Feeds::GCF::Exception::ExcCCBroker);

use strict qw(vars);
use warnings ( FATAL => 'all' );

use Data::Dumper;
use Readonly;
use POSIX qw(strftime);

use Feeds::GCF::Constants qw(:GENERIC :STD_TABLE :STD_TYPE :SYBASE_SQL);
use Feeds::GCF::Util qw(hostname now today redo_sql);
use Feeds::Utils::CDSBrokerConfig;
use Feeds::Utils::CDSBrokerMapper;
use Feeds::GCF::Viper;
use Feeds::GCF::Exception::ExcCCBroker;
use Feeds::GCF::Util qw(send_email);








sub check_tranche_tier_cd{
 
    my $self   = shift;
    my $asset  = shift;
    my $n_data = shift;
    
    if ($n_data->{n_tbl}->{inst_nm}
        && $n_data->{n_tbl}->{inst_nm} =~ /(\d*)\-(\d*)%/
        && $asset->{mapping}->{contract}->{idx_ticker}
        && $asset->{mapping}->{contract}->{series}){
        my $attach = $1;
        my $detach = $2;
        if ($attach and $detach) {
        } else {
            push @{ $asset->{exc_cd} }, 'WTC';
            return;
        }
        my $sql=<<"SQL";
         
          select distinct tranche_cd 
          from n_tranche_contract
          where attach=$1
            and detach=$2
            and idx_ticker='$asset->{mapping}->{contract}->{idx_ticker}'
            and series='$asset->{mapping}->{contract}->{series}'
            and info_src_cd!='GFI'
               
SQL

     my $res=$self->{dbh}->selectall_hashes($sql);
     
     if (scalar @$res >1){
        my $address="adder.zeng\@thomsonreuters.com";
        my $email_subject="duplicate tranche_cd found";
        my $email_body="oid:[$n_data->{n_tbl}->{oid}], sql:\n".$sql;
        send_email($address,"$email_subject","$email_body",'text/html',undef,"data-ops\@reuters.net");
     }
  
     if ( scalar @$res == 0 ) {
       
#      $self->{log}->info("can't find tranche info from n_tranche_contract, try to find in mapping table");
        if ($attach and $detach) {
        } else {
            push @{ $asset->{exc_cd} }, 'WTC';
            return;
        }
     
      $sql=<<"SQL";
         
          select trch_tier as  tranche_cd
          from m_trch_tier_mapping
          where attach=$1
            and detach=$2
            and idx_ticker='$asset->{mapping}->{contract}->{idx_ticker}'
            and series='$asset->{mapping}->{contract}->{series}'
                           
SQL

       $res=$self->{dbh}->selectall_hashes($sql);
        
     }
     
    
       if ( scalar @$res >= 1 ) {
       
        $asset->{mapping}->{contract}->{attach}= $attach;
        $asset->{mapping}->{contract}->{detach}= $detach;
        $asset->{mapping}->{contract}->{ric_tranche_cd}=$res->[0]->{tranche_cd};
        ($asset->{mapping}->{contract}->{idx_trch_leg_nm}
         ,$asset->{mapping}->{contract}->{idx_trch_tier_cd}) = $self->_get_trch_info($asset->{mapping}->{contract}->{ric_tranche_cd});
         
         return;
        
     }
     
     
   }
   
    push @{ $asset->{exc_cd} }, 'WTC';
    
    
    
    
  
    
   
    
    
}

sub _get_trch_info{
    
    my $self = shift;
    my $dest_ric_tier_cd = shift;
    
    my $count=0;
    foreach my $ric_tier_cd (sort keys %{$self->{tranche_tier_cd_def}}){
        $count++;
        if ($dest_ric_tier_cd eq $ric_tier_cd){
            return ($count,  $self->{tranche_tier_cd_def}->{$ric_tier_cd});
        }
    }
}



sub _get_trch_info_by_name{
    
    my $self = shift;
    my $dest_ric_tier_cd = shift;
    
    my $count=0;
    foreach my $ric_tier_cd (sort keys %{$self->{tranche_tier_cd_def}}){
        $count++;
        if ($dest_ric_tier_cd eq $ric_tier_cd){
            return ($count,  $self->{tranche_tier_cd_def}->{$ric_tier_cd});
        }
    }
}
1;
